package com.example.root.xposd_hook_new;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class HookTest implements IXposedHookLoadPackage {
    /*android.app.ActivityManager*/
    /*IActivityManagerSingleton*/
    public static final String SHARK = "Rachel_xposed";
//    public static final String CLASS_NAME = "com.google.android.apps.inputmethod.pinyin.preference.SettingsActivity";
    public static final String CLASS_NAME = "android.app.Activity";
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
        Log.i(SHARK, "pkg:"+loadPackageParam.packageName);
        if (loadPackageParam.packageName.equals("com.google.android.inputmethod.pinyin")) {
            Log.d(SHARK, "hooking...");
            XposedBridge.log("hooking...");
            XposedHelpers.findAndHookMethod(CLASS_NAME, loadPackageParam.classLoader,"startActivity", Intent.class,
            new XC_MethodHook() {

                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    Log.d(SHARK, "beforeHookedMethod...");
                    super.beforeHookedMethod(param);
                    Log.i(SHARK, "beforeHookedMethod:"+param);
                    Log.i(SHARK, "beforeHookedMethod:"+param.getResult());
                    Log.i(SHARK, "beforeHookedMethod:"+param.getExtra());
                    Log.i(SHARK, "beforeHookedMethod:"+param.getThrowable());
                    Bundle extra = new Bundle();
                    Log.i(SHARK, "beforeHookedMethod:"+extra);
                    Log.i(SHARK, "beforeHookedMethod:"+param.args[0]);
                    Intent newIntent= (Intent) param.args[0];
                    //        //构造函数中指定ACTION是Intent.ACTION_VIEW，即用于打开浏览器
//                    Intent intent = new Intent(Intent.ACTION_VIEW);
//                    //指定要打开的网址
//                    intent.setData(Uri.parse("https://www.baidu.com"));
//                    startActivity(intent);
//                    Intent newIntent =  param.getExtra();
                    Log.i(SHARK, "beforeHookedMethod:"+newIntent.getAction());
                    if (Intent.ACTION_VIEW.equals(newIntent.getAction())){
                        Log.i(SHARK, "beforeHookedMethod:"+newIntent.getData());
                        newIntent.setData(Uri.parse("https://www.baidu.com"));
                        Log.i(SHARK, "beforeHookedMethod:"+newIntent.getData());
//                    param.setResult("shark chilli");
                        param.args[0]=newIntent;
                        Log.i(SHARK, "beforeHookedMethod:成功");
                        Log.i(SHARK, "beforeHookedMethod:"+param.args[0]);
                    }
                }

                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    Log.d(SHARK, "afterHookedMethod...");
                    if (param.args!=null){
                        Log.i(SHARK, "afterHookedMethod:"+param.args[0]);
                    }

////                    Object obj = param.getResult();
//                    Log.i(SHARK, "afterHookedMethod:"+param);
//                    Log.i(SHARK, "afterHookedMethod:"+param.getResult());
//                    Log.i(SHARK, "afterHookedMethod:"+param.getExtra());
//                    Log.i(SHARK, "afterHookedMethod:"+param.getThrowable());
//                    Bundle extra = new Bundle();
//                    Log.i(SHARK, "afterHookedMethod:"+extra);
//                    Log.i(SHARK, "afterHookedMethod:"+param.args[0]);
//                    Intent newIntent= (Intent) param.args[0];
//                    //        //构造函数中指定ACTION是Intent.ACTION_VIEW，即用于打开浏览器
////                    Intent intent = new Intent(Intent.ACTION_VIEW);
////                    //指定要打开的网址
////                    intent.setData(Uri.parse("https://www.baidu.com"));
////                    startActivity(intent);
////                    Intent newIntent =  param.getExtra();
//                    Log.i(SHARK, "afterHookedMethod:"+newIntent.getAction());
//                    if (Intent.ACTION_VIEW.equals(newIntent.getAction())){
//                        Log.i(SHARK, "afterHookedMethod:"+newIntent.getData());
//                        newIntent.setData(Uri.parse("https://www.baidu.com"));
//                        Log.i(SHARK, "afterHookedMethod:"+newIntent.getData());
////                    param.setResult("shark chilli");
//                        param.args[0]=newIntent;
//                        Log.i(SHARK, "afterHookedMethod:成功");
//                        Log.i(SHARK, "afterHookedMethod:"+param.args[0]);
//                    }
                }

            });

        }

    }

}